package com.molyfun.parents.activity.signup;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.ListPopupWindow;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.fragment.dialog.SexChooseDialogFragment;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.bean.SignUpDetailResponse;
import com.molyfun.parents.rest.bean.SignUpFixParentInfoBean;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.utils.IDCardVerify;
import com.molyfun.parents.utils.VerificationInfomationUtils;
import com.molyfun.parents.view.ThreeTabBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SignUpEditeParentInfoActivity extends BaseActivity implements View.OnClickListener {

    private String TAG = "SignUpEditeParentInfoActivity";
    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.edite_student_info_submit_button)
    TextView mSubmitButton;
    @InjectView(R.id.layout_mother_info)
    LinearLayout mLauoytMother;
    @InjectView(R.id.layout_father_info)
    LinearLayout mLauoytFather;
    @InjectView(R.id.base_title)
    TextView mTitle;

    private int mCurrentCategory;
    private String student_Id;

    private List<SignUpDetailResponse.DataBean.ContactlistpageBean> mList;
    private EditText motherRelationship;
    private EditText motherName;
    private EditText motherIdentity;
    private TextView motherSex;
    private TextView motherEducate;
    private EditText motherOccupation;
    private EditText motherQq;
    private EditText motherPhone;
    private TextView fatherRelationship;
    private EditText fatherName;
    private TextView fatherSex;
    private EditText fatherIdentity;
    private TextView fatherEducate;
    private EditText fatherOccupation;
    private EditText fatherQq;
    private EditText fatherPhone;
    private ListPopupWindow mSexListPop;
    private ListPopupWindow mFatherSexListPop;
    private ArrayList<String> sexList = null;
    private ArrayList<String> educateList = null;
    private String momName;
    private String momSex;
    private String momQq;
    private String momRelationShip;
    private String momPhone;
    private String momEducate;
    private String momOccupation;
    private String dadaName;
    private String dadaSex;
    private String dadQq;
    private String dadRelationship;
    private String dadPhone;
    private String dadEducate;
    private String dadOccupation;
    ////0:男 1:女
    private String mMotherSexStr = "-1";
    private String mFatherSexStr = "-1";

    public static void startActivity(Context context, int category, SignUpDetailResponse.DataBean.StudentenrolpageBean studentenrolpage, ArrayList<SignUpDetailResponse.DataBean.ContactlistpageBean> contactlistpage) {
        Intent intent = new Intent();
        intent.putExtra("category", category);
        intent.putExtra("student_Id", studentenrolpage.getId());
        intent.putParcelableArrayListExtra("contactlistpage", contactlistpage);
        intent.setClass(context, SignUpEditeParentInfoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_edite_parent_info);
        ButterKnife.inject(this);
        initView();

    }

    private void initView() {
        getSupportActionBar().setTitle("");
        mTitle.setText("编辑家长信息");
        sexList = new ArrayList<>();
        sexList.add("男");
        sexList.add("女");
        educateList = new ArrayList<>();
        educateList.add("小学");
        educateList.add("初中");
        educateList.add("高中");
        educateList.add("中专");
        educateList.add("大专");
        educateList.add("本科");
        educateList.add("硕士");
        educateList.add("博士");
        Intent intent = getIntent();
        mCurrentCategory = intent.getIntExtra("category", 0);
        student_Id = intent.getStringExtra("student_Id");
        motherName = (EditText) mLauoytMother.findViewById(R.id.parente_info_name);
        motherIdentity = (EditText) mLauoytMother.findViewById(R.id.parente_info_identity);
        motherSex = (TextView) mLauoytMother.findViewById(R.id.parent_info_sex);
        motherRelationship = (EditText) mLauoytMother.findViewById(R.id.parente_info_relationship);
        motherEducate = (TextView) mLauoytMother.findViewById(R.id.parente_info_educate);
        motherOccupation = (EditText) mLauoytMother.findViewById(R.id.parente_info_occupation);
        motherQq = (EditText) mLauoytMother.findViewById(R.id.parente_info_qq);
        motherPhone = (EditText) mLauoytMother.findViewById(R.id.parente_info_connect);

        fatherRelationship = (TextView) mLauoytFather.findViewById(R.id.parente_info_relationship);
        fatherName = (EditText) mLauoytFather.findViewById(R.id.parente_info_name);
        fatherSex = (TextView) mLauoytFather.findViewById(R.id.parent_info_sex);
        fatherIdentity = (EditText) mLauoytFather.findViewById(R.id.parente_info_identity);
        fatherEducate = (TextView) mLauoytFather.findViewById(R.id.parente_info_educate);
        fatherOccupation = (EditText) mLauoytFather.findViewById(R.id.parente_info_occupation);
        fatherQq = (EditText) mLauoytFather.findViewById(R.id.parente_info_qq);
        fatherPhone = (EditText) mLauoytFather.findViewById(R.id.parente_info_connect);
        mList = getIntent().getParcelableArrayListExtra("contactlistpage");
        if (mList.size() > 0 && mList.size() == 1) {
            motherRelationship.setText(mList.get(0).getRelationship());
            motherName.setText(mList.get(0).getName());
            motherSex.setText(mList.get(0).getSex());
            mMotherSexStr = mList.get(0).getSex();
            motherIdentity.setText(mList.get(0).getCardid());
            motherEducate.setText(mList.get(0).getEducation());
            motherOccupation.setText(mList.get(0).getOccupation());
            motherQq.setText(mList.get(0).getQq());
            motherPhone.setText(mList.get(0).getPhone());
        } else if (mList.size() == 2) {
            motherRelationship.setText(mList.get(0).getRelationship());
            motherName.setText(mList.get(0).getName());
            motherSex.setText(mList.get(0).getSex());
            motherIdentity.setText(mList.get(0).getCardid());
            motherEducate.setText(mList.get(0).getEducation());
            motherOccupation.setText(mList.get(0).getOccupation());
            motherQq.setText(mList.get(0).getQq());
            motherPhone.setText(mList.get(0).getPhone());
            fatherRelationship.setText(mList.get(1).getRelationship());
            fatherName.setText(mList.get(1).getName());
            fatherSex.setText(mList.get(1).getSex());
            mFatherSexStr = mList.get(1).getSex();
            fatherIdentity.setText(mList.get(1).getCardid());
            fatherEducate.setText(mList.get(1).getEducation());
            fatherOccupation.setText(mList.get(1).getOccupation());
            fatherQq.setText(mList.get(1).getQq());
            fatherPhone.setText(mList.get(1).getPhone());
        } else {
            return;
        }

        mBack.setOnClickListener(this);
        mSubmitButton.setOnClickListener(this);
        motherSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new SexChooseDialogFragment.SexChooseDialogBuilder()
                        .setSexChooseCallback(new SexChooseDialogFragment.SexChooseCallback() {
                            @Override
                            public void onSexChoose(boolean isMale) {
                                if(isMale){
                                    mMotherSexStr = "男";
                                    motherSex.setText("男");
                                }else{
                                    mMotherSexStr = "女";
                                    motherSex.setText("女");
                                }
                            }
                        })
                        .build(getSupportFragmentManager().beginTransaction());

//
//                mSexListPop = new ListPopupWindow(SignUpEditeParentInfoActivity.this);
//                selectorOperation(sexList, mSexListPop, motherSex);
//                mSexListPop.show();


            }
        });
        fatherSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new SexChooseDialogFragment.SexChooseDialogBuilder()
                        .setSexChooseCallback(new SexChooseDialogFragment.SexChooseCallback() {
                            @Override
                            public void onSexChoose(boolean isMale) {
                                if(isMale){
                                    mFatherSexStr = "男";
                                    fatherSex.setText("男");
                                }else{
                                    mFatherSexStr = "女";
                                    fatherSex.setText("女");
                                }
                            }
                        })
                        .build(getSupportFragmentManager().beginTransaction());

//                mFatherSexListPop = new ListPopupWindow(SignUpEditeParentInfoActivity.this);
//                selectorOperation(sexList, mFatherSexListPop, fatherSex);
//                mFatherSexListPop.show();
            }
        });
        motherEducate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListPopupWindow mMotherEducatePop = new ListPopupWindow(SignUpEditeParentInfoActivity.this);
                selectorOperation(educateList, mMotherEducatePop, motherEducate);
                mMotherEducatePop.show();
            }
        });
        fatherEducate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListPopupWindow mFatherEducatePop = new ListPopupWindow(SignUpEditeParentInfoActivity.this);
                selectorOperation(educateList, mFatherEducatePop, fatherEducate);
                mFatherEducatePop.show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.edite_student_info_submit_button:
                if (mCurrentCategory == ThreeTabBar.ThreeTabChooseTab.BAR_ONE) {

                } else {
                    submiteParentInfo();
                }
                break;
        }
    }

    private List<SignUpFixParentInfoBean.ContactlistBean> contactlist = new ArrayList<>();

    private void submiteParentInfo() {
        //IDCardVerify.verifyIdNumber
        guardianInfo();


        momSex = motherSex.getText().toString().trim();

        dadaSex = fatherSex.getText().toString().trim();


        String motherName = momName;
        String motherIdentity = momIdentity;
        String motherRelationship = momRelationShip;
        String motherEdute = momEducate;
        String motherProfession = momOccupation;
        String motherQQ = momQq;
        String motherConnect = momPhone;

        String fatherName = dadaName;
//        String fatherSex = editParenceFatherSex.getText().toString().trim();
        String fatherIdentity = dadIdentitiy;
        String fatherRelationship = dadRelationship;
        String fatherEdute = dadEducate;
        String fatherPression = dadOccupation;
        String fatherQQ = dadQq;
        String fatherConnect = dadPhone;

        if(motherEdute.equals("请选择")){
            motherEdute = "";
        }
        if(fatherEdute.equals("请选择")){
            fatherEdute = "";
        }

        boolean isHasParents = false;

        if ((!TextUtils.isEmpty(motherName) && !mMotherSexStr.equals("-1") && !TextUtils.isEmpty(motherIdentity)
                && !TextUtils.isEmpty(motherRelationship)
                && !TextUtils.isEmpty(motherEdute) && !TextUtils.isEmpty(motherProfession)
                && !TextUtils.isEmpty(motherQQ) && !TextUtils.isEmpty(motherConnect))){

            if(!IDCardVerify.verifyIdNumber(motherIdentity)){
                toast("监护人信息身份证有误");
                return;
            }
            if(!VerificationInfomationUtils.isMobileNO(motherConnect)){
                toast("监护人电话号码有误");
                return;
            }
            momBean();
            isHasParents = true;
        }else{
            if(!((TextUtils.isEmpty(motherName) && mMotherSexStr.equals("-1") && TextUtils.isEmpty(motherIdentity)
                    && TextUtils.isEmpty(motherRelationship)
                    && TextUtils.isEmpty(motherEdute) && TextUtils.isEmpty(motherProfession)
                    && TextUtils.isEmpty(motherQQ) && TextUtils.isEmpty(motherConnect)))){
                if(TextUtils.isEmpty(motherName)){
                    toast("监护人姓名不能为空");
                    return;
                }
                if(mMotherSexStr.equals("-1")){
                    toast("监护人性别必选");
                    return;
                }
                if(TextUtils.isEmpty(motherIdentity)){
                    toast("监护人信息身份证有误");
                    return;
                }
                if(TextUtils.isEmpty(motherRelationship)){
                    toast("监护人与学生关系必填");
                    return;
                }
                if(TextUtils.isEmpty(motherEdute)){
                    toast("监护人学历必选");
                    return;
                }
                if(TextUtils.isEmpty(motherProfession)){
                    toast("监护人职业必填");
                    return;
                }
                if(TextUtils.isEmpty(motherQQ)){
                    toast("监护人QQ必填");
                    return;
                }
                if(TextUtils.isEmpty(motherConnect)){
                    toast("监护人联系电话必填");
                    return;
                }

            }
        }

        if(!mFatherSexStr.equals("-1")&& !TextUtils.isEmpty(fatherIdentity)
                && !TextUtils.isEmpty(fatherRelationship)
                && !TextUtils.isEmpty(fatherEdute) && !TextUtils.isEmpty(fatherPression)
                && !TextUtils.isEmpty(fatherQQ) && !TextUtils.isEmpty(fatherConnect)) {
            if(!IDCardVerify.verifyIdNumber(fatherIdentity)){
                toast("监护人信息身份证有误");
                return;
            }
            if(!VerificationInfomationUtils.isMobileNO(fatherConnect)){
                toast("监护人电话号码有误");
                return;
            }
            dadBean();
            isHasParents = true;
        }else{
            if(!((TextUtils.isEmpty(fatherName) && mFatherSexStr.equals("-1") && TextUtils.isEmpty(fatherIdentity)
                    && TextUtils.isEmpty(fatherRelationship)
                    && TextUtils.isEmpty(fatherEdute) && TextUtils.isEmpty(fatherPression)
                    && TextUtils.isEmpty(fatherQQ) && TextUtils.isEmpty(fatherConnect)))){
                if(TextUtils.isEmpty(fatherName)){
                    toast("监护人姓名不能为空");
                    return;
                }
                if(mFatherSexStr.equals("-1")){
                    toast("监护人性别必选");
                    return;
                }
                if(TextUtils.isEmpty(fatherIdentity)){
                    toast("监护人信息身份证有误");
                    return;
                }
                if(TextUtils.isEmpty(fatherRelationship)){
                    toast("监护人与学生关系必填");
                    return;
                }
                if(TextUtils.isEmpty(fatherEdute)){
                    toast("监护人学历必选");
                    return;
                }
                if(TextUtils.isEmpty(fatherPression)){
                    toast("监护人职业必填");
                    return;
                }
                if(TextUtils.isEmpty(fatherQQ)){
                    toast("监护人QQ必填");
                    return;
                }
                if(TextUtils.isEmpty(fatherConnect)){
                    toast("监护人联系电话必填");
                    return;
                }
            }
        }

        if(!isHasParents){
            toast("监护人信息必须完整填写一个");
            return;
        }


        SignUpFixParentInfoBean.StudentEnrolBean studentEnrolBean = new SignUpFixParentInfoBean.StudentEnrolBean();
        studentEnrolBean.setId(student_Id);
        SignUpFixParentInfoBean signUpFixParentInfoBean = new SignUpFixParentInfoBean();
        signUpFixParentInfoBean.setStudentEnrol(studentEnrolBean);
        if (contactlist.size() > 0) {
            signUpFixParentInfoBean.setContactlist(contactlist);
        } else {
            toast("监护人信息必须完整填写一项");
            return;
        }
        showProgressDialog();
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().signUpFixparentInfo(signUpFixParentInfoBean);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getMsg() != null) {
                    Message uimessage = new Message();
                    uimessage.what = EventDispatcherEnum.UI_EVENT_SIGNUP_EDITE_PARENT_INFO;
                    System.out.println("EventDispatcherEnum.UI_EVENT_SIGNUP_EDITE_PARENT_INFO  ----->");
                    MFPApplication.getInstance().getEventDispatcher().sendMessage(uimessage);
                    finish();
                } else {
                    toast(response.body().getMsg() + "");
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("提交异常");
            }
        });
    }

    private void selectorOperation(final List<String> list, final ListPopupWindow popupWindow, final TextView tv_view) {
        popupWindow.setAdapter(new ArrayAdapter<String>(this, R.layout.layout_parity_pop, list));
        popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setAnchorView(tv_view);//设置ListPopupWindow的锚点，即关联PopupWindow的显示位置和这个锚点
        popupWindow.setModal(true);//设置是否是模式
        popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                tv_view.setText(list.get(position));
                popupWindow.dismiss();
            }
        });
    }

    public void momBean() {
        SignUpFixParentInfoBean.ContactlistBean contactlistMotherBean = new SignUpFixParentInfoBean.ContactlistBean();
        contactlistMotherBean.setCardid(momIdentity);
        contactlistMotherBean.setName(momName);
        contactlistMotherBean.setSex(momSex);
        contactlistMotherBean.setQq(momQq);
        contactlistMotherBean.setRelationship(momRelationShip);
        contactlistMotherBean.setPhone(momPhone);
        contactlistMotherBean.setEducation(momEducate);
        contactlistMotherBean.setOccupation(momOccupation);
        contactlist.add(contactlistMotherBean);
    }

    public void dadBean() {
        SignUpFixParentInfoBean.ContactlistBean contactlistFatherBean = new SignUpFixParentInfoBean.ContactlistBean();
        contactlistFatherBean.setCardid(dadIdentitiy);
        contactlistFatherBean.setName(dadaName);
        contactlistFatherBean.setSex(dadaSex);
        contactlistFatherBean.setQq(dadQq);
        contactlistFatherBean.setRelationship(dadRelationship);
        contactlistFatherBean.setPhone(dadPhone);
        contactlistFatherBean.setEducation(dadEducate);
        contactlistFatherBean.setOccupation(dadOccupation);
        contactlist.add(contactlistFatherBean);
    }

    private String momIdentity = "";
    private String dadIdentitiy = "";

    public void guardianInfo() {

      /*  if (!TextUtils.isEmpty(motherIdentity.getText().toString().trim()) && IDCardVerify.verifyIdNumber(motherIdentity.getText().toString().trim())) {
            momIdentity = motherIdentity.getText().toString().trim();
        } else {
            if (TextUtils.isEmpty(motherIdentity.getText().toString().trim())) {
                momIdentity = "";
            } else {
                toast("身份证有误");
                return;
            }
        }
        if (!TextUtils.isEmpty(fatherIdentity.getText().toString().trim()) && IDCardVerify.verifyIdNumber(fatherIdentity.getText().toString().trim())) {
            dadIdentitiy = fatherIdentity.getText().toString().trim();
        } else {
            if (TextUtils.isEmpty(fatherIdentity.getText().toString().trim())) {
                dadIdentitiy = "";
            } else {
                toast("身份证有误");
                return;
            }
        }*/
        momIdentity = motherIdentity.getText().toString().trim();
        momName = motherName.getText().toString().trim();
        momSex = motherSex.getText().toString().trim();
        momQq = motherQq.getText().toString().trim();
        momRelationShip = motherRelationship.getText().toString().trim();
        momPhone = motherPhone.getText().toString().trim();
        momEducate = motherEducate.getText().toString().trim();
        momOccupation = motherOccupation.getText().toString().trim();

        dadIdentitiy = fatherIdentity.getText().toString().trim();
        dadaName = fatherName.getText().toString().trim();
        dadaSex = fatherSex.getText().toString().trim();
        dadQq = fatherQq.getText().toString().trim();
        dadRelationship = fatherRelationship.getText().toString().trim();
        dadPhone = fatherPhone.getText().toString().trim();
        dadEducate = fatherEducate.getText().toString().trim();
        dadOccupation = fatherOccupation.getText().toString().trim();
    }
}
